
/**
 * Title:        <p>
 * Description:  <p>
 * Copyright:    Copyright (c) <p>
 * cleaned up flow so that login prompt reappears after bad login
 *
 */
package com.timber.login;

import java.awt.*;
import javax.swing.*;
import javax.swing.border.*;
import javax.swing.table.*;
import java.awt.event.*;

import pv.jfcx.*;

import com.timber.util.*;
import com.borland.jbcl.layout.*;
import javax.swing.event.*;


/**
 * login dialog
 */
public class LoginDialog extends JDialog
{


  public LoginDialog(String userName) {
    this();
    tUser.setText(userName);
    tUser.setEnabled(false);
  }

  public LoginDialog()
  {
    try {
      jbInit();
      setSize(380, 170);
      pack();
      Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
      int screenHeight = d.height;
      int screenWidth = d.width;
      int myWidth = getWidth();
      int myHeight = getHeight();
      setLocation( ( screenWidth - myWidth ) / 2,
		   ( screenHeight - myHeight ) / 2 );

    }
      catch(Exception e) {
      e.printStackTrace();
    }

  }

  public static final int DO_LOGIN	= 1;
  public static final int CHANGE_PWD	= 2;
  public static final int CANCEL	= 3;

  protected JPanel pParent;
  protected JTextField tUser = new JTextField();
  JPanel pUserName = new JPanel();

  JPanel pUser = new JPanel();

  JLabel lUser = new JLabel();
  JLabel lPassword = new JLabel();
  JPasswordField tPassword = new JPasswordField();
  String newPassword;
  JLabel image = new JLabel();
  Border border1;
  Border border2;
  FlowLayout flowLayout3 = new FlowLayout();
  BorderLayout borderLayout1 = new BorderLayout();
  JPanel actionP = new JPanel();
  JButton btnLogon = new JButton();
  String password = "";
  private int exitCode;
  private int actionCode = 0; // 0 - do login; 1 - change password
  protected JButton btnChange = new JButton();
  protected JButton btnExit = new JButton();
  protected FlowLayout flowLayout1 = new FlowLayout();
  protected Border border3;


  private void jbInit() throws Exception
  {


    pParent = (JPanel)this.getContentPane();
    border1 = BorderFactory.createEtchedBorder(Color.white,new Color(134, 134, 134));
    border2 = BorderFactory.createCompoundBorder(BorderFactory.createEtchedBorder(Color.white,new Color(134, 134, 134)),BorderFactory.createEmptyBorder(5,5,5,5));
    border3 = BorderFactory.createEtchedBorder(Color.white,new Color(142, 142, 142));
    pParent.setAlignmentX((float) 0.0);
    pParent.setAlignmentY((float) 0.0);
    pParent.setBorder(border2);
    pParent.setMinimumSize(new Dimension(300, 610));
    pParent.setPreferredSize(new Dimension(340, 160));
    pParent.setLayout(borderLayout1);
    tUser.setPreferredSize(new Dimension(140, 21));
    pUser.setLayout(flowLayout1);
    lUser.setFont(new java.awt.Font("Dialog", 1, 11));
    lUser.setMaximumSize(new Dimension(640, 17));
    lUser.setPreferredSize(new Dimension(80, 17));
    lUser.setText("User Name");
    lPassword.setText("Password");
    lPassword.setFont(new java.awt.Font("Dialog", 1, 11));
    lPassword.setMaximumSize(new Dimension(789, 17));
    lPassword.setPreferredSize(new Dimension(80, 17));
    tPassword.setPreferredSize(new Dimension(140, 21));
    tPassword.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(ActionEvent e) {
        tPassword_actionPerformed(e);
      }
    });
    this.setTitle("iDelta Login");
    pUserName.setPreferredSize(new Dimension(220, 60));
    pUserName.setLayout(flowLayout3);
    flowLayout3.setAlignment(FlowLayout.LEFT);
    flowLayout3.setHgap(0);
    image.setMaximumSize(new Dimension(91, 120));
    image.setPreferredSize(new Dimension(91, 100));
    image.setIcon(ImageMgr.getIcon(ImageMgr.DBLOGO_ICON ));

    btnLogon.addActionListener(new java.awt.event.ActionListener() {

      public void actionPerformed(ActionEvent e) {
        btnLogon_actionPerformed(e);
      }
    });
    btnLogon.setFont(new java.awt.Font("Dialog", 1, 11));
    btnLogon.setText("Log on");
    pUser.setPreferredSize(new Dimension(320, 100));
    btnChange.setFont(new java.awt.Font("Dialog", 1, 11));
    btnChange.setText("Change Pwd");
    btnChange.addActionListener(new java.awt.event.ActionListener()
    {
      public void actionPerformed(ActionEvent e)
      {
        btnChange_actionPerformed(e);
      }
    });
    btnExit.setFont(new java.awt.Font("Dialog", 1, 11));
    btnExit.setText("Exit");
    btnExit.addActionListener(new java.awt.event.ActionListener()
    {
      public void actionPerformed(ActionEvent e)
      {
        btnExit_actionPerformed(e);
      }
    });
    borderLayout1.setVgap(10);
    pParent.add(pUser, BorderLayout.CENTER);
    pUser.add(image, null);
    pUser.add(pUserName, null);
    pUserName.add(lUser, null);
    pUserName.add(tUser, null);
    pUserName.add(lPassword, null);
    pUserName.add(tPassword, null);
    pParent.add(actionP, BorderLayout.SOUTH);
    actionP.add(btnLogon, null);
    actionP.add(btnChange, null);
    actionP.add(btnExit, null);

  }

  void tPassword_actionPerformed(ActionEvent e){btnLogon_actionPerformed(e);}

  protected void processWindowEvent(WindowEvent e) {
    super.processWindowEvent(e);
    if(e.getID() == WindowEvent.WINDOW_CLOSING) {
      actionCode = this.CANCEL;
      //hide();
      System.exit(0);
    }
  }

  public String getPassword(){return password;}
  public String getUserName(){return tUser.getText();}
  public int getActionCode(){return actionCode;}



  private boolean validateLogin()
  {
    char [] arr = tPassword.getPassword();
    password = "";
    for (int i=0; i<arr.length; i++)
      password = password + arr[i];

    if (tUser.getText().length() <1 || password.length() <1)
    {
      JOptionPane.showMessageDialog(this, "Please enter user name and password");
      return false;
    }
    return true;

  }



  void btnExit_actionPerformed(ActionEvent e)
  {
    actionCode = this.CANCEL;
    //hide();
    System.exit(0);
  }
  void btnLogon_actionPerformed(ActionEvent e)
  {
    actionCode = this.DO_LOGIN;
    if (!validateLogin())
      return;
    setVisible( false );
  }

  void btnChange_actionPerformed(ActionEvent e)
  {
    actionCode = this.CHANGE_PWD;
    if (!validateLogin())
      return;
    setVisible( false );
  }




}
